package org.point.common.util;

import java.text.NumberFormat;
import java.util.Locale;

/**
 * 字符串工具类
 */
public class DealString {
	
	public DealString() {
	}

	/**
	 * 判断字符串是否为空，并删除首尾空格
	 * @param tempSql
	 * @return
	 */
	public static String convertNullCode(String tempSql){
		if (tempSql==null) tempSql="";
		return tempSql;
	}
	/**
	 * 字符串替换操作
	 * @param originString 原字符串
	 * @param oldString 被替换字符串
	 * @param newString 替换字符串
	 * @return 替换操作后的字符串
	 */
	public static String replace(String originString,String oldString,String newString){
		String getstr = originString;
		while(getstr.indexOf(oldString)>-1){
			getstr = getstr.substring(0,getstr.indexOf(oldString)) + newString + getstr.substring(getstr.indexOf(oldString)+oldString.length(),getstr.length());
		}
		return getstr;
	}

	/**
	 * 代码转换 从srcCode转换为destCode
	 * @param srcCode 原编码
	 * @param destCode 目标编码
	 * @param strTmp 要转换的字符串
	 * @return
	 */
	public static String convertCode(String srcCode,String destCode,String strTmp){
	    String returnString = convertNullCode(strTmp);
	    try{
	    	byte[] ascii=returnString.getBytes(srcCode);
	        returnString =new String(ascii,destCode);
	    }catch(Exception e){
	    	e.printStackTrace();
	    }
	    return returnString;
	}
	
	/**
	 * 替换非法字符
	 * @param input
	 * @return
	 */
	public static String convertHtml(String input){
		StringBuffer returnString = new StringBuffer(input.length());

	    char ch = ' ';
	    for (int i = 0;i<input.length();i++){

	    	ch = input.charAt( i);

	    	if (ch == '<'){
	    		returnString = returnString.append("&lt");
	    	}else if (ch == '>'){
	    		returnString = returnString.append("&gt");
	    	}else if (ch == ' '){
	    		returnString = returnString.append("&nbsp");
	    	}else if (ch == '\\'){
	    		returnString = returnString.append("&acute");
	    	}else{
	    		returnString = returnString.append(ch);
	    	}
	    }
	    return returnString.toString();
	}

	/**
	 * 数字的金额表达式
	 * @param num
	 * @return
	 */
	public static String convertNumToMoney(int num){
		NumberFormat formatc = NumberFormat.getCurrencyInstance(Locale.CHINA);
		String strcurr = formatc.format(num);
		return strcurr;
	}

	public static void main(String args[]){
		DealString.convertNumToMoney(1234566);
	}
}
